﻿
#include "TestTime.h"
#include "pch.h"
int frequency_of_primes(int n) {
	int i, j;
	int freq = n - 1;
	for (i = 2; i <= n; ++i) for (j = sqrt(i); j > 1; --j) if (i%j == 0) { --freq; break; }
	return freq;
}
void testTime() {
	// 基于当前系统的当前日期/时间
	time_t now = time(0);

	char timestamp[64];
	sprintf(timestamp, "%d", now);

	cout << "本地时间戳：" << timestamp << endl;

	// 把 now 转换为字符串形式
	char* dt = ctime(&now);

	cout << "本地日期和时间：" << dt << endl;

	// 把 now 转换为 tm 结构
	tm *gmtm = localtime(&now);
	dt = asctime(gmtm);
	cout << "UTC 日期和时间：" << dt << endl;

	tm *ltm = localtime(&now);

	// 输出 tm 结构的各个组成部分
	cout << "年: " << 1900 + ltm->tm_year << endl;
	cout << "月: " << 1 + ltm->tm_mon << endl;
	cout << "日: " << ltm->tm_mday << endl;
	cout << "时间: " << ltm->tm_hour << ":";
	cout << ltm->tm_min << ":";
	cout << ltm->tm_sec << endl;

	time_t timep;
	time (&timep);
	char tmp[64];
	strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S",localtime(&timep) );
	cout << "UTC 日期和时间：" << tmp << endl;		


	time_t start_t, end_t;
	double diff_t;

	printf("程序启动...\n");
	time(&start_t);

	printf("休眠 5 秒...\n");
	this_thread::sleep_for(std::chrono::milliseconds(3000));

	time(&end_t);
	diff_t = difftime(end_t, start_t);

	printf("执行时间 = %f\n", diff_t);
	printf("程序退出...%d \n",end_t-start_t);


	long i = 10000000L;
	clock_t start, finish;
	double duration;
	printf("Time to do %ld empty loops is ", i);
	start = clock();
	while (i--)
		finish = clock();
	duration = (double)(finish - start);
	printf("%f 毫秒/n", duration);

	clock_t t;
	int f;
	t = clock();
	printf("Calculating...\n");
	f = frequency_of_primes(99999);
	printf("The number of primes lower than 100,000 is: %d\n", f);
	t = clock() - t;
	printf("It took me %d clicks (%f seconds).\n", t, ((float)t) / CLOCKS_PER_SEC);
	

	char st[sizeof(long)];
	printf("sizeof(long): %d \n", sizeof(long));//4

	sprintf(st, "%d", t);
	//clock_t转成string
	printf("clock_t to string: %s \n", st);

	string s = st;

	printf("%p ,%p", s, st);

	//system("pause");

	//c++11
	using namespace std::chrono;
	//duration
	typedef std::chrono::duration<int> seconds_type;
	typedef std::chrono::duration<int, std::milli> milliseconds_type;
	typedef std::chrono::duration<int, std::ratio<60 * 60>> hours_type;

	hours_type h_oneday(24);                  // 24h
	seconds_type s_oneday(60 * 60 * 24);          // 86400s
	milliseconds_type ms_oneday(s_oneday);    // 86400000ms

	seconds_type s_onehour(60 * 60);            // 3600s
  //hours_type h_onehour (s_onehour);          // NOT VALID (type truncates), use:
	hours_type h_onehour(std::chrono::duration_cast<hours_type>(s_onehour));
	milliseconds_type ms_onehour(s_onehour);  // 3600000ms (ok, no type truncation)

	std::cout << ms_onehour.count() << "ms in 1h" << std::endl;

	milliseconds_type foo(1000); // 1 second
	foo *= 60;

	std::cout << "duration (in periods): ";
	std::cout << foo.count() << " milliseconds.\n";

	std::cout << "duration (in seconds): ";
	std::cout << foo.count() * milliseconds_type::period::num / milliseconds_type::period::den;
	std::cout << " seconds.\n";

	//time_point
	system_clock::time_point tp_epoch;    // epoch value

	time_point <system_clock, seconds_type> tp_seconds(seconds_type(1));

	system_clock::time_point tp(tp_seconds);

	std::cout << "1 second since system_clock epoch = ";
	std::cout << tp.time_since_epoch().count();
	std::cout << " system_clock periods." << std::endl;

	// display time_point:
	std::time_t tt = system_clock::to_time_t(tp);
	std::cout << "time_point tp is: " << ctime(&tt);

	//clock
	std::chrono::duration<int, std::ratio<60 * 60 * 24> > one_day(1);
	system_clock::time_point today = system_clock::now();
	system_clock::time_point tomorrow = today + one_day;

	//std::time_t tt;

	tt = system_clock::to_time_t(today);
	std::cout << "today is: " << ctime(&tt);

	tt = system_clock::to_time_t(tomorrow);
	std::cout << "tomorrow will be: " << ctime(&tt);

	system_clock::duration d = system_clock::now().time_since_epoch();
	minutes min = duration_cast<minutes>(d);
	seconds sec = duration_cast<seconds>(d);
	milliseconds mil = duration_cast<milliseconds>(d);
	microseconds mic = duration_cast<microseconds>(d);
	nanoseconds nan = duration_cast<nanoseconds>(d);
	cout << min.count() << "分钟" << endl;
	cout << sec.count() << "秒" << endl;
	cout << mil.count() << "毫秒" << endl;
	cout << mic.count() << "微妙" << endl;
	cout << nan.count() << "纳秒" << endl;

	
}